home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / string / RCS / string.c,v < prev   
Encoding:
Text File  |  1988-07-22  |  6.1 KB  |  317 lines

  1. head     1.2;
  2. access   ;
  3. symbols  ;
  4. locks    ; strict;
  5. comment  @ * @;
  6.  
  7.  
  8. 1.2
  9. date     88.07.22.08.59.52;  author ouster;  state Exp;
  10. branches ;
  11. next     1.1;
  12.  
  13. 1.1
  14. date     88.04.25.13.09.17;  author ouster;  state Exp;
  15. branches ;
  16. next     ;
  17.  
  18.  
  19. desc
  20. @@
  21.  
  22.  
  23. 1.2
  24. log
  25. @ADded tests for strpbrk, strspn, strcspn.
  26. @
  27. text
  28. @/* 
  29.  * string.c --
  30.  *
  31.  *    This file contains a program that exercises the string
  32.  *    library procedures.  Invoke it with no parameters;  it
  33.  *    will print messages on stderr for any problems it detects
  34.  *    with the string procedures.
  35.  *
  36.  * Copyright 1988 Regents of the University of California
  37.  * Permission to use, copy, modify, and distribute this
  38.  * software and its documentation for any purpose and without
  39.  * fee is hereby granted, provided that the above copyright
  40.  * notice appear in all copies.  The University of California
  41.  * makes no representations about the suitability of this
  42.  * software for any purpose.  It is provided "as is" without
  43.  * express or implied warranty.
  44.  */
  45.  
  46. #ifndef lint
  47. static char rcsid[] = "$Header: string.c,v 1.1 88/04/25 13:09:17 ouster Exp $ SPRITE (Berkeley)";
  48. #endif not lint
  49.  
  50. #include <stdio.h>
  51. #include <string.h>
  52.  
  53. #define error(string) \
  54.     fprintf(stderr, string); \
  55.     exit(1);
  56.  
  57. main()
  58. {
  59.     int result = 0;
  60.     char test[100];
  61.     void *x;
  62.  
  63.     /*
  64.      * strcmp
  65.      */
  66.  
  67.     if (strcmp("abcdef", "abcdef") != 0) {
  68.     error("strcmp error 1\n");
  69.     }
  70.     if (strcmp("abcd", "abcdef") >= 0) {
  71.     error("strcmp error 2\n");
  72.     }
  73.     if (strcmp("12345", "12354") >= 0) {
  74.     error("strcmp error 3\n");
  75.     }
  76.     if (strcmp("xyz", "abc") <= 0) {
  77.     error("strcmp error 4\n");
  78.     }
  79.  
  80.     /*
  81.      * strncmp
  82.      */
  83.  
  84.     if (strncmp("abc\0x", "abc\0y", 6) != 0) {
  85.     error("strncmp error 1\n");
  86.     }
  87.     if (strncmp("abcdefg", "abcxyz", 3) != 0) {
  88.     error("strncmp error 2\n");
  89.     }
  90.     if (strncmp("abcdefg", "abcxyz", 4) >= 0) {
  91.     error("strncmp error 3\n");
  92.     }
  93.     if (strncmp("abcdefg", "abcabc", 4) <= 0) {
  94.     error("strncmp error 4\n");
  95.     }
  96.  
  97.     /*
  98.      * strcpy
  99.      */
  100.  
  101.     if (strcpy(test, "") != test) {
  102.     error("strcpy error 1\n");
  103.     }
  104.     if (test[0] != 0) {
  105.     error("strcpy error 2\n");
  106.     }
  107.     strcpy(test, "abcdefg");
  108.     if (strcmp(test, "abcdefg") != 0) {
  109.     error("strcpy error 3\n");
  110.     }
  111.  
  112.     /*
  113.      * strncpy
  114.      */
  115.  
  116.     strcpy(test, "abcdefg");
  117.     if (strncpy(test, "xyzqrs", 3) != test) {
  118.     error("strncpy error 1\n");
  119.     }
  120.     if (bcmp(test, "xyzdefg", 8) != 0) {
  121.     error("strncpy error 2\n");
  122.     }
  123.     strncpy(test, "12", 6);
  124.     if (bcmp(test, "12\0\0\0\0g", 8) != 0) {
  125.     error("strncpy error 3\n");
  126.     }
  127.  
  128.     /*
  129.      * strcat
  130.      */
  131.  
  132.     strcpy(test, "qrs");
  133.     if (strcat(test, "tuv") != test) {
  134.     error("strcat error 1\n");
  135.     }
  136.     if (strcmp(test, "qrstuv") != 0) {
  137.     error("strcat error 2\n");
  138.     }
  139.     test[0] = 0;
  140.     strcat(test, "12345");
  141.     if (strcmp(test, "12345") != 0) {
  142.     error("strcat error 3\n");
  143.     }
  144.     strcat(test, "");
  145.     if (strcmp(test, "12345") != 0) {
  146.     error("strcat error 4\n");
  147.     }
  148.  
  149.     /*
  150.      * strncat
  151.      */
  152.  
  153.     strcpy(test, "Now is the time");
  154.     if (strncat(test, " for all good men", 5) != test) {
  155.     error("strncat error 1\n");
  156.     }
  157.     if (strcmp(test, "Now is the time for ") != 0) {
  158.     error("strncat error 2\n");
  159.     }
  160.     strcpy(test, "abc");
  161.     strncat(test, "xyz", 6);
  162.     if (strcmp(test, "abcxyz") != 0) {
  163.     error("strncat error 3\n");
  164.     }
  165.  
  166.     /*
  167.      * strchr & index
  168.      */
  169.  
  170.     strcpy(test, "abcabcabcdef");
  171.     if (strchr(test, 'a') != test) {
  172.     error("strchr error 1");
  173.     }
  174.     if (strchr(test, 'c') != &test[2]) {
  175.     error("strchr error 2");
  176.     }
  177.     if (strchr(test, 'x') != 0) {
  178.     error("strchr error 3");
  179.     }
  180.     if (strchr(test, 0) != &test[12]) {
  181.     error("strchr error 4");
  182.     }
  183.     if (index(test, 'a') != test) {
  184.     error("index error 1");
  185.     }
  186.     if (index(test, 'c') != &test[2]) {
  187.     error("index error 2");
  188.     }
  189.     if (index(test, 'x') != 0) {
  190.     error("index error 3");
  191.     }
  192.     if (index(test, 0) != &test[12]) {
  193.     error("index b error 4");
  194.     }
  195.  
  196.     /*
  197.      * strrchr & rindex
  198.      */
  199.  
  200.     if (strrchr(test, 'a') != &test[6]) {
  201.     error("strrchr error 1\n");
  202.     }
  203.     if (strrchr(test, 0) != &test[12]) {
  204.     error("strrchr error 2\n");
  205.     }
  206.     if (strrchr(test, 'x') != 0) {
  207.     error("strrchr error 3\n");
  208.     }
  209.     if (rindex(test, 'a') != &test[6]) {
  210.     error("rindex error 1\n");
  211.     }
  212.     if (rindex(test, 0) != &test[12]) {
  213.     error("rindex error 2\n");
  214.     }
  215.     if (rindex(test, 'x') != 0) {
  216.     error("rindex error 3\n");
  217.     }
  218.  
  219.     /*
  220.      * strstr
  221.      */
  222.  
  223.     strcpy(test, "Now is the time for all good men");
  224.     if (strstr(test, "Now") != test) {
  225.     error("strstr error 1\n");
  226.     }
  227.     if (strstr(test, "good men") != &test[24]) {
  228.     error("strstr error 2\n");
  229.     }
  230.     if (strstr(test, "now") != 0) {
  231.     error("strstr error 3\n");
  232.     }
  233.     if (strstr(test, "") != test) {
  234.     error("strstr error 4\n");
  235.     }
  236.  
  237.     /*
  238.      * strlen
  239.      */
  240.  
  241.     if (strlen("") != 0) {
  242.     error("strlen error 1\n");
  243.     }
  244.     if (strlen("abc") != 3) {
  245.     error("strlen error 2\n");
  246.     }
  247.     if (strlen("Now is the time") != 15) {
  248.     error("strlen error 3\n");
  249.     }
  250.  
  251.     /*
  252.      * strpbrk
  253.      */
  254.  
  255.     strcpy(test, "12341234567");
  256.     if (strpbrk(test, "4567") != &test[3]) {
  257.     error("strpbrk error 1\n");
  258.     }
  259.     if (strpbrk(test, "") != 0) {
  260.     error("strpbrk error 2\n");
  261.     }
  262.     if (strpbrk(test, "abcdefgh") != 0) {
  263.     error("strpbrk error 3\n");
  264.     }
  265.  
  266.     /*
  267.      * strspn
  268.      */
  269.  
  270.     strcpy(test, "Now is the time for");
  271.     if (strspn(test, " ") != 0) {
  272.     error("strspn error 1\n");
  273.     }
  274.     if (strspn(test, "wnoNsi ") != 7) {
  275.     error("strspn error 2\n");
  276.     }
  277.     if (strspn(test, "") != 0) {
  278.     error("strspn error 3\n");
  279.     }
  280.     if (strspn(test, "aeioufhmNrstw ") != 19) {
  281.     error("strspn error 4\n");
  282.     }
  283.  
  284.     /*
  285.      * strcspn
  286.      */
  287.  
  288.     strcpy(test, "Now is the time for");
  289.     if (strcspn(test, " ") != 3) {
  290.     error("strcspn error 1\n");
  291.     }
  292.     if (strcspn(test, "N") != 0) {
  293.     error("strcspn error 2\n");
  294.     }
  295.     if (strcspn(test, "") != 19) {
  296.     error("strcspn error 3\n");
  297.     }
  298.     if (strcspn(test, "ABC;") != 19) {
  299.     error("strcspn error 4\n");
  300.     }
  301.  
  302.     return result;
  303. }
  304. @
  305.  
  306.  
  307. 1.1
  308. log
  309. @Initial revision
  310. @
  311. text
  312. @d20 1
  313. a20 1
  314. static char rcsid[] = "$Header: proto.c,v 1.2 88/03/11 08:39:08 ouster Exp $ SPRITE (Berkeley)";
  315. d222 51
  316. @
  317.